var person = {
	init:function(){
        this.layuiUse();	
	},
	layuiUse:function(){
		layui.use(['layer', 'form','jquery'], function(){
            var layer = layui.layer,
                form = layui.form,
                $ = layui.$;

            let isTokenNow = public.isToken();
            if(window.location.href.indexOf('findPassword')==-1){
                if(isTokenNow!=10000){
                    layer.msg('<p class="colorSystem font16 alignC mt15">请您先登录~</p>',{time:600},function(){
                        window.location.href="../../html/login/login.html";
                    });
                }
            }
            

            $('p[name="nicknameCookie"]').html('欢迎您，'+GetCookie('nickname')+' !');
            $('p[name="mobileCookie"]').html(GetCookie('mobile'));

            form.verify({
                //找回密码--表单验证
                userPhone: function(value, item){ 
                    if(!(/^1[3456789]\d{9}$/.test(value))){ 
                        return '请输入正确的手机号码';
                    } 
                },
                code: function(value, item){ 
                    if(!(value)){ 
                        return '请输入验证码';
                    } 
                },
                pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
                ],
                surePass: function(value, item){ 
                    if(value!=$('input[name="userPassword"]').val()){ 
                        return '两次密码输入的不一致';
                    } 
                },
                userName:function(value, item){
                    if(!(value)){ 
                        return '请输入收货人姓名';
                    } 
                },
                userAddress:function(value, item){
                    if(!(value)){ 
                        return '请输入收货地址';
                    } 
                },
            }); 
            //找回密码--监听提交
            form.on('submit(formDemo)', function(data){
                $.ajax({
                    type: 'post',
                    url:link+'/mobile/retrievePwd',
                    data:data.field,
                    dataType: 'json',
                    success: function (res) {
                        if(res.success){
                            if(window.location.href.indexOf('login')!=-1){
                                layer.msg(res.message,{icon:6,time:600},function(){
                                    window.location.href="../../html/login/login.html";
                                });
                            }else if(window.location.href.indexOf('person')!=-1){
                                layer.msg(res.message,{icon:6,time:600},function(){
                                    window.location.href="../../html/person/person.html";
                                });
                            }
                            
                        }else{
                            layer.msg(res.message);
                        }
                    },
                    error:function(res){
                        layer.msg(res.message);
                    },
                });
                return false;
            });
            $('body').on('click','#code',function(){
                person.codeButton();
            });
            $('body').on('click','li[name="logout"]',function(){
                person.logout();
            });
            $('span[name="userMobile"]').html(mobile);
            $('input[name="mobile"]').val(mobile);

            //收货地址
            if(GetCookie('address')){
                $('input[name="addressUserName"]').val(GetCookie('addressUserName'));
                $('input[name="addressUserPhone"]').val(GetCookie('addressUserPhone'));
                $('textarea[name="address"]').val(GetCookie('address'));
            }
            form.on('submit(formAddress)', function(data){
                layer.msg('地址保存成功',{ shift: -1, time: 1000 },function(){
                    SetCookieDomain('addressUserName',data.field.addressUserName,'');
                    SetCookieDomain('addressUserPhone',data.field.addressUserPhone,'');
                    SetCookieDomain('address',data.field.address,'');
                    history.go(-1);
                });
                return false;
            });
        });
    },
    //获取验证码
    codeButton:function(){
        let ajaxKeyData={
            'mobile':$('input[name="mobile"]').val(),
            'mobileType':'Android'
        };
        //第一步获取key
        if(!(/^1[3456789]\d{9}$/.test(ajaxKeyData.mobile))){ 
            layer.msg('请输入正确的手机号码');
        }else{
            $.ajax({
                type: 'post',
                url:link+'/mobile/getMobileKey',
                data:ajaxKeyData,
                dataType: 'json',
                success: function (res) {
                    if(res.success){
                        //第二步获取验证码
                        let ajaxCodeData={
                            'mobile':$('input[name="mobile"]').val(),
                            'sign':res.entity,
                            'mobileType':'Android'
                        }
                        $.ajax({
                            type: 'post',
                            url:link+'/mobile/retrievePwdSend',
                            data:ajaxCodeData,
                            dataType: 'json',
                            success: function (data) {
                                if(res.success){
                                    layer.msg(data.message);
                                    var code = $("#code");
                                    code.attr("disabled","disabled");
                                    setTimeout(function(){
                                        code.css("opacity","0.8");
                                    },1000)
                                    var time = 60;
                                    var set=setInterval(function(){
                                    code.val("("+--time+")秒后重新获取");
                                    }, 1000);
                                    setTimeout(function(){
                                    code.attr("disabled",false).val("重新获取验证码");
                                    clearInterval(set);
                                    }, 60000);
                                }else{
                                    layer.msg(data.message);
                                }
                            },
                            error:function(res){
                                layer.msg(data.message);
                            },
                        });
                    }else{
                        layer.msg(res.message);
                    }
                },
                error:function(res){
                    
                },
            });
        }
    },
    //退出登录
    logout:function(){
        $.ajax({
            type: 'post',
            url:link+'/mobile/userLogout',
            data:{
                'token':token
            },
            dataType: 'json',
            success: function (res) {
                if(res.code=='10000'){
                    layer.msg(res.message,{time:600},function(){
                        clearAllCookie();
                        window.location.href="../../html/login/login.html";
                    });
                }else{
                    layer.msg(res.message);
                }
            },
            error:function(res){
                layer.msg(res.message);
            },
        });
    },
};
person.init();

          